package cn.yuanqiao.system.service.impl;

import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.utils.SnowflakeIdGenerator;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.system.domain.SysLogininfor;
import cn.yuanqiao.system.mapper.TenantLogininforMapper;
import cn.yuanqiao.system.service.ITenantLogininforService;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 系统访问日志情况信息 服务层处理
 *
 * @author ruoyi
 */
@Service
public class TenantLogininforServiceImpl implements ITenantLogininforService
{

    @Autowired
    private TenantLogininforMapper logininforMapper;

    /**
     * 新增系统登录日志
     *
     * @param logininfor 访问日志对象
     */
    @Override
    public int insertLogininfor(SysLogininfor logininfor)
    {
        String tenantCode = SecurityUtils.getTenantCode();
        logininfor.setTenantCode(tenantCode);
        logininfor.setInfoId(SnowflakeIdGenerator.generateId());
        if(logininfor.getLogLevel() ==null){
            logininfor.setLogLevel(Integer.valueOf(logininfor.getStatus()));
        }
        return logininforMapper.insertLogininfor(logininfor);
    }

    /**
     * 查询系统登录日志集合
     *
     * @param logininfor 访问日志对象
     * @return 登录记录集合
     */
    @Override
    public List<SysLogininfor> selectLogininforList(SysLogininfor logininfor)
    {
        String tenantCode = SecurityUtils.getTenantCode();
        logininfor.setTenantCode(tenantCode);
        return logininforMapper.selectLogininforList(logininfor);
    }

    /**
     * 批量删除系统登录日志
     *
     * @param infoIds 需要删除的登录日志ID
     * @return 结果
     */
    @Override
    public int deleteLogininforByIds(Long[] infoIds)
    {
        String tenantCode = SecurityUtils.getTenantCode();
        int i = logininforMapper.selectLogininforcout(infoIds, tenantCode);
        if (i>0){
            throw new ServiceException("未审计的数据不可以删除,请先进行审计!");
        }

        return logininforMapper.deleteLogininforByIds(infoIds,tenantCode);
    }

    /**
     * 清空系统登录日志
     */
    @Override
    public void cleanLogininfor()
    {
        String tenantCode = SecurityUtils.getTenantCode();
        logininforMapper.cleanLogininfor(tenantCode);
    }

    @Override
    public List<SysLogininfor> selectLogininforListBySysauditor(SysLogininfor logininfor) {
        String tenantCode = SecurityUtils.getTenantCode();
        logininfor.setTenantCode(tenantCode);
        return logininforMapper.selectLogininforListBySysauditor(logininfor);
    }

    @Override
    public List<SysLogininfor> selectLogininforListBySyssso(SysLogininfor logininfor) {
        String tenantCode = SecurityUtils.getTenantCode();
        logininfor.setTenantCode(tenantCode);
        return logininforMapper.selectLogininforListBySyssso(logininfor);
    }

    @Override
    public List<Long[]> loginTongji(SysLogininfor logininfor) {
        String tenantCode = SecurityUtils.getTenantCode();
        logininfor.setTenantCode(tenantCode);
        Map<String,Long> map=logininforMapper.loginTongji(logininfor);;
        List list=new ArrayList(6);
        for (Map.Entry<String,Long> entry : map.entrySet()) {
            Long [] value=new Long[3];
            value[0]=Long.valueOf(String.valueOf(entry.getKey().charAt(0)));
            value[1]=Long.valueOf(String.valueOf(entry.getKey().charAt(1)));
            value[2]=entry.getValue();
            list.add(value);
        }
        return list;
    }

    @Override
    public List<Long[]> loginTongjiBySysauditor(SysLogininfor logininfor) {
        String tenantCode = SecurityUtils.getTenantCode();
        logininfor.setTenantCode(tenantCode);
        Map<String,Long> map=logininforMapper.loginTongjiBySysauditor(logininfor);;
        List list=new ArrayList(6);
        for (Map.Entry<String,Long> entry : map.entrySet()) {
            Long [] value=new Long[3];
            value[0]=Long.valueOf(String.valueOf(entry.getKey().charAt(0)));
            value[1]=Long.valueOf(String.valueOf(entry.getKey().charAt(1)));
            value[2]=Long.valueOf(String.valueOf(entry.getValue()));
            list.add(value);
        }
        return list;
    }

    @Override
    public List<Long[]> loginTongjiBySyssso(SysLogininfor logininfor) {
        String tenantCode = SecurityUtils.getTenantCode();
        logininfor.setTenantCode(tenantCode);
        Map<String,Long> map=logininforMapper.loginTongjiBySyssso(logininfor);;
        List list=new ArrayList(6);
        for (Map.Entry<String,Long> entry : map.entrySet()) {
            Long [] value=new Long[3];
            value[0]=Long.valueOf(String.valueOf(entry.getKey().charAt(0)));
            value[1]=Long.valueOf(String.valueOf(entry.getKey().charAt(1)));
            value[2]=Long.valueOf(String.valueOf(entry.getValue()));
            list.add(value);
        }
        return list;
    }

    /**
     * 修改审计状态
     * @param jsonObject
     * @return
     */
    @Override
    public int updatAuditByIds(JSONObject jsonObject)
    {
        String tenantCode = SecurityUtils.getTenantCode();
        JSONArray operIds = jsonObject.getJSONArray("infoIds");
        String auditOpinion = jsonObject.getString("auditOpinion");
        List<Long> longs = operIds.toJavaList(Long.class);
        return logininforMapper.updatAuditByIds(longs,tenantCode,auditOpinion);
    }
}
